package com.live;

import static com.easefun.polyv.livecommon.module.config.PLVLiveScene.CLOUDCLASS;
import static com.easefun.polyv.livecommon.module.config.PLVLiveScene.ECOMMERCE;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.easefun.polyv.livecloudclass.scenes.PLVLCCloudClassActivity;
import com.easefun.polyv.livecommon.module.config.PLVLiveChannelConfig;
import com.easefun.polyv.livecommon.module.config.PLVLiveChannelConfigFiller;
import com.easefun.polyv.livecommon.module.config.PLVLiveScene;
import com.easefun.polyv.livecommon.module.utils.result.PLVLaunchResult;
import com.easefun.polyv.liveecommerce.scenes.PLVECLiveEcommerceActivity;
import com.easefun.polyv.livestreamer.scenes.PLVLSLiveStreamerActivity;
import com.easefun.polyv.streameralone.scenes.PLVSAStreamerAloneActivity;
import com.live.databinding.ActivityMainBinding;
import com.plv.foundationsdk.utils.PLVUtils;
import com.plv.livescenes.config.PLVLiveChannelType;
import com.plv.livescenes.feature.login.IPLVSceneLoginManager;
import com.plv.livescenes.feature.login.PLVLiveLoginResult;
import com.plv.livescenes.feature.login.PLVSceneLoginManager;
import com.plv.livescenes.feature.login.model.PLVLoginVO;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "emmmmmmm";
    IPLVSceneLoginManager manager=new PLVSceneLoginManager();
    private final String app_key="b2e9e7132af14184a5534dcaea459cfb";
    private final String app_id="g55ar4as51";
    private final String user_id="1fea220456";
    private PLVLiveScene curScene = ECOMMERCE;
    private final String password="a00000";
    //自定义的
    private ActivityMainBinding binding;
public void toast(String s){
    Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
}
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        PLVLiveChannelConfigFiller.setupAccount(user_id,app_id,app_key);

        binding.kaibo.setOnClickListener(view -> manager.loginStreamerNew(binding.channelId.getText().toString(), password, new IPLVSceneLoginManager.OnLoginListener<PLVLoginVO>() {
            @Override
            public void onLoginSuccess(PLVLoginVO loginVO) {
                PLVLiveChannelType liveChannelType = loginVO.getLiveChannelTypeNew();
                if (PLVLiveChannelType.PPT.equals(liveChannelType)) {
                    //进入手机开播三分屏场景
                    boolean isOpenCamera = "N".equals(loginVO.getIsOnlyAudio());
                    PLVLaunchResult launchResult = PLVLSLiveStreamerActivity.launchStreamer(
                            MainActivity.this,
                            loginVO.getChannelId(),
                            loginVO.getInteractUid(),
                            loginVO.getNickname(),
                            loginVO.getTeacherAvatar(),
                            loginVO.getTeacherActor(),
                            loginVO.getRole(),
                            loginVO.getColinMicType(),
                            true,
                            isOpenCamera,
                            true
                    );
                    if (!launchResult.isSuccess()) {
                        onLoginFailed(launchResult.getErrorMessage(), launchResult.getError());
                    }
                } else if (PLVLiveChannelType.ALONE.equals(liveChannelType)) {
                    //进入手机开播纯视频场景
                    PLVLaunchResult launchResult = PLVSAStreamerAloneActivity.launchStreamer(
                            MainActivity.this,
                            loginVO.getChannelId(),
                            loginVO.getInteractUid(),
                            loginVO.getTeacherNickname(),
                            loginVO.getTeacherAvatar(),
                            loginVO.getTeacherActor(),
                            loginVO.getChannelName(),
                            loginVO.getRole(),
                            loginVO.getColinMicType()
                    );
                    if (!launchResult.isSuccess()) {
                        onLoginFailed(launchResult.getErrorMessage(), launchResult.getError());
                    }
                } else {
                    String errorMsg = getResources().getString(com.easefun.polyv.livecommon.R.string.plv_scene_login_toast_streamer_no_support_type);
                    onLoginFailed(errorMsg, new Throwable(errorMsg));
                }
            }
            @Override
            public void onLoginFailed(String s, Throwable throwable) {
                Log.i(TAG, "onLoginFailed: "+s+throwable.getMessage());
            }
        }));
        binding.lessionLive.setOnClickListener(view -> manager.loginLiveNew(app_id, app_key, user_id, binding.channelId.getText().toString(), new IPLVSceneLoginManager.OnLoginListener<PLVLiveLoginResult>() {
            @Override
            public void onLoginSuccess(PLVLiveLoginResult plvLiveLoginResult) {
                curScene=CLOUDCLASS;
                PLVLiveChannelType channelType = plvLiveLoginResult.getChannelTypeNew();
                toast("类型:"+channelType);
                live(channelType);
            }
            @Override
            public void onLoginFailed(String s, Throwable throwable) {
                Log.i(TAG, "onLoginFailed: "+s+throwable.getMessage());
            }
        }));
        binding.sealLive.setOnClickListener(view -> manager.loginLiveNew(app_id, app_key, user_id, binding.channelId.getText().toString(), new IPLVSceneLoginManager.OnLoginListener<PLVLiveLoginResult>() {
            @Override
            public void onLoginSuccess(PLVLiveLoginResult plvLiveLoginResult) {
                curScene=ECOMMERCE;
                PLVLiveChannelType channelType = plvLiveLoginResult.getChannelTypeNew();
                toast("类型:"+channelType);
                live(channelType);
            }
            @Override
            public void onLoginFailed(String s, Throwable throwable) {
                Log.i(TAG, "onLoginFailed: "+s+throwable.getMessage());
            }
        }));

        binding.teacher.setOnClickListener(view -> {
            toast("未实现");
        });

        binding.student.setOnClickListener(view -> {
            toast("未实现");
        });
    }

    private void live(PLVLiveChannelType channelType) {
        switch (curScene) {
            //进入云课堂场景
            case CLOUDCLASS:
                if (PLVLiveScene.isCloudClassSceneSupportType(channelType)) {
                    PLVLaunchResult launchResult = PLVLCCloudClassActivity.launchLive(MainActivity.this, binding.channelId.getText().toString(), channelType, getViewerId(), getViewerName(), getViewerAvatar());
                    if (!launchResult.isSuccess()) {
                        toast(launchResult.getErrorMessage());
                    }
                } else {
                    toast(getResources().getString(com.easefun.polyv.livecommon.R.string.plv_scene_login_toast_cloudclass_no_support_type));
                }
                break;
            //进入直播带货场景
            case ECOMMERCE:
                if (PLVLiveScene.isLiveEcommerceSceneSupportType(channelType)) {
                    PLVLaunchResult launchResult = PLVECLiveEcommerceActivity.launchLive(MainActivity.this, binding.channelId.getText().toString(), getViewerId(), getViewerName(), getViewerAvatar());
                    if (!launchResult.isSuccess()){
                        toast(launchResult.getErrorMessage());
                    }
                } else {
                    toast( getResources().getString(com.easefun.polyv.livecommon.R.string.plv_scene_login_toast_liveecommerce_no_support_type));
                }
                break;
            default:
                break;
        }
    }
    private String getViewerId() {
        return PLVUtils.getAndroidId(this) + "";
    }

    private String getViewerName() {
        return "观众" + getViewerId();
    }

    private String getViewerAvatar(){
        return "";
    }
}